using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    [Serializable()]
    public partial class CCheck : CBaseDynamic, IComparable<CCheck>
    {
        #region Constructors
        //Public (Copy Constructor)
        public CCheck(CCheck original, CDataSrc target)
        {
            //Database Connection
            m_dataSrc = target;

            //Copy fields
            _checkUserId = original.CheckUserId;
            _checkStarted = original.CheckStarted;
            _checkFinished = original.CheckFinished;
            _checkInserts = original.CheckInserts;
            _checkUpdates = original.CheckUpdates;
            _checkDeletes = original.CheckDeletes;
            _checkApiCalls = original.CheckApiCalls;
            _checkIsIncremental = original.CheckIsIncremental;
            _checkErrors = original.CheckErrors;
        }

        //Protected (Datareader/Dataset)
        protected CCheck(CDataSrc dataSrc, IDataReader dr) : base(dataSrc, dr) {}
        protected CCheck(CDataSrc dataSrc, DataRow     dr) : base(dataSrc, dr) {}
        #endregion

        #region Default Values
        protected override void InitValues_Auto()
        {
            //Null-Equivalent values (except String.Empty, as nulls tend to be inconvenient for string types)
            _checkId = long.MinValue;
            _checkUserId = long.MinValue;
            _checkStarted = DateTime.MinValue;
            _checkFinished = DateTime.MinValue;
            _checkInserts = int.MinValue;
            _checkUpdates = int.MinValue;
            _checkDeletes = int.MinValue;
            _checkApiCalls = int.MinValue;
            _checkIsIncremental = false;
            _checkErrors = string.Empty;
        }
        #endregion

        #region Members
        protected long _checkId;
        protected long _checkUserId;
        protected DateTime _checkStarted;
        protected DateTime _checkFinished;
        protected int _checkInserts;
        protected int _checkUpdates;
        protected int _checkDeletes;
        protected int _checkApiCalls;
        protected bool _checkIsIncremental;
        protected string _checkErrors;
        #endregion

        #region Properties - Column Values
        //Primary Key Column (ReadOnly)
        public long CheckId    {   get { return _checkId;   } }

        //Table Columns (Read/Write)
        public long CheckUserId    {   get { return _checkUserId; } set { _checkUserId = value; }    }
        public DateTime CheckStarted    {   get { return _checkStarted; } set { _checkStarted = value; }    }
        public DateTime CheckFinished    {   get { return _checkFinished; } set { _checkFinished = value; }    }
        public int CheckInserts    {   get { return _checkInserts; } set { _checkInserts = value; }    }
        public int CheckUpdates    {   get { return _checkUpdates; } set { _checkUpdates = value; }    }
        public int CheckDeletes    {   get { return _checkDeletes; } set { _checkDeletes = value; }    }
        public int CheckApiCalls    {   get { return _checkApiCalls; } set { _checkApiCalls = value; }    }
        public bool CheckIsIncremental    {   get { return _checkIsIncremental; } set { _checkIsIncremental = value; }    }
        public string CheckErrors    {   get { return _checkErrors; } set { _checkErrors = value; }    }

        //View Columns (ReadOnly)

        #endregion

        #region MustOverride Methods
        //Schema Information
        public const string TABLE_NAME      = "Checks";
        public const string VIEW_NAME       = "";         //Used to override this.ViewName { get }
        public const string ORDER_BY_COLS   = "CheckId DESC";   //See the CompareTo method below (Sql-based sorting should match In-Memory sorting)
        public const string SORTING_COLUMN  = "";
        public override string TableName {    get { return TABLE_NAME; } }
        protected override string OrderByColumns {    get { return ORDER_BY_COLS; } }

        //CompareTo Interface (Default Sort Order)
        public int CompareTo(CCheck other) {   return this.CheckId.CompareTo(other.CheckId) *-1;  }

        //Primary Key Information
        public const string PRIMARY_KEY_NAME = "CheckId";
        protected override bool InsertPrimaryKey {  get { return false;    }   }
        protected override string PrimaryKeyName {  get { return PRIMARY_KEY_NAME;    }    }
        protected override object PrimaryKeyValue 
        {
              get { return _checkId; }
              set { _checkId = Convert.ToInt64(value); }
        }

        //Factory Methods - Object
        protected override CBase MakeFrom(DataRow row)      {   return new CCheck(this.DataSrc, row);  }
        protected override CBase MakeFrom(IDataReader dr)   {   return new CCheck(this.DataSrc, dr);   }

        //Factory Methods - List
        protected override IList MakeList()                 {   return new CCheckList();               }
        protected override IList MakeList(int capacity)     {   return new CCheckList(capacity);       }

        //Convert from ADO to .Net
        protected override void ReadColumns(IDataReader dr)
        {
            _checkId = CAdoData.GetLong(dr, "CheckId");
            _checkUserId = CAdoData.GetLong(dr, "CheckUserId");
            _checkStarted = CAdoData.GetDate(dr, "CheckStarted");
            _checkFinished = CAdoData.GetDate(dr, "CheckFinished");
            _checkInserts = CAdoData.GetInt(dr, "CheckInserts");
            _checkUpdates = CAdoData.GetInt(dr, "CheckUpdates");
            _checkDeletes = CAdoData.GetInt(dr, "CheckDeletes");
            _checkApiCalls = CAdoData.GetInt(dr, "CheckApiCalls");
            _checkIsIncremental = CAdoData.GetBool(dr, "CheckIsIncremental");
            _checkErrors = CAdoData.GetStr(dr, "CheckErrors");
        }
        protected override void ReadColumns(DataRow dr)
        {
            _checkId = CAdoData.GetLong(dr, "CheckId");
            _checkUserId = CAdoData.GetLong(dr, "CheckUserId");
            _checkStarted = CAdoData.GetDate(dr, "CheckStarted");
            _checkFinished = CAdoData.GetDate(dr, "CheckFinished");
            _checkInserts = CAdoData.GetInt(dr, "CheckInserts");
            _checkUpdates = CAdoData.GetInt(dr, "CheckUpdates");
            _checkDeletes = CAdoData.GetInt(dr, "CheckDeletes");
            _checkApiCalls = CAdoData.GetInt(dr, "CheckApiCalls");
            _checkIsIncremental = CAdoData.GetBool(dr, "CheckIsIncremental");
            _checkErrors = CAdoData.GetStr(dr, "CheckErrors");
        }

        //Parameters for Insert/Update    
        protected override CNameValueList ColumnNameValues()
        {
            CNameValueList data = new CNameValueList();
            data.Add("CheckId", CAdoData.NullVal(_checkId));
            data.Add("CheckUserId", CAdoData.NullVal(_checkUserId));
            data.Add("CheckStarted", CAdoData.NullVal(_checkStarted));
            data.Add("CheckFinished", CAdoData.NullVal(_checkFinished));
            data.Add("CheckInserts", CAdoData.NullVal(_checkInserts));
            data.Add("CheckUpdates", CAdoData.NullVal(_checkUpdates));
            data.Add("CheckDeletes", CAdoData.NullVal(_checkDeletes));
            data.Add("CheckApiCalls", CAdoData.NullVal(_checkApiCalls));
            data.Add("CheckIsIncremental", CAdoData.NullVal(_checkIsIncremental));
            data.Add("CheckErrors", CAdoData.NullVal(_checkErrors));
            return data;
        }
        #endregion

        #region Queries - SelectAll/SelectWhere (inherited methods, cast only)
        //Select Queries - Non-Paged
        public new CCheckList SelectAll()                                                          {    return (CCheckList)base.SelectAll();                              }
        public new CCheckList SelectAll(string orderBy)                                            {    return (CCheckList)base.SelectAll(orderBy);                       }
        public new CCheckList SelectWhere(string colName, ESign sign, object colValue)             {    return (CCheckList)base.SelectWhere(colName, sign, colValue);     }
        public new CCheckList SelectWhere(CCriteria where)                                         {    return (CCheckList)base.SelectWhere(where);                       }
        public new CCheckList SelectWhere(CCriteriaList where)                                     {    return (CCheckList)base.SelectWhere(where);                       }
        public new CCheckList SelectWhere(CCriteriaList where, string tableOrJoin)                 {    return (CCheckList)base.SelectWhere(where, tableOrJoin);          }
        public new CCheckList SelectWhere(CCriteriaList where, string tableOrJoin, string orderBy) {    return (CCheckList)base.SelectWhere(where, tableOrJoin, orderBy); }
        [Obsolete("Arbitrary where-clause is not necessary portable or safe from sql injection attacks. Consider using the parameterised query interfaces")]
        public new CCheckList SelectWhere(string unsafeWhereClause)                                {    return (CCheckList)base.SelectWhere(unsafeWhereClause);           }
        public     CCheckList SelectById(long checkId)                              {    return (CCheckList)base.SelectById(checkId);       }
        public     CCheckList SelectByIds(List<long> ids)                        {    return (CCheckList)base.SelectByIds(ids);                         }

        //Select Queries - Paged
        public new CCheckList SelectAll(  CPagingInfo pi)                                              {    return (CCheckList)base.SelectAll(  pi);                              }
        public new CCheckList SelectWhere(CPagingInfo pi, string name, ESign sign, object value)       {    return (CCheckList)base.SelectWhere(pi, name, sign, value);           }
        public new CCheckList SelectWhere(CPagingInfo pi, CCriteria criteria)                          {    return (CCheckList)base.SelectWhere(pi, criteria);                    }
        public new CCheckList SelectWhere(CPagingInfo pi, CCriteriaList criteria)                      {    return (CCheckList)base.SelectWhere(pi, criteria);                    }
        public new CCheckList SelectWhere(CPagingInfo pi, CCriteriaList criteria, string viewOrJoin)   {    return (CCheckList)base.SelectWhere(pi, criteria, viewOrJoin);        }
        public     CCheckList SelectByIds(CPagingInfo pi, List<long> ids)            {    return (CCheckList)base.SelectByIds(pi, ids);                         }

        //Select Queries - Transactional
        public new CCheckList SelectAll(                                                                                           IDbTransaction tx)  {    return (CCheckList)base.SelectAll(tx);                                                     }
        public new CCheckList SelectAll(string orderBy,                                                                            IDbTransaction tx)  {    return (CCheckList)base.SelectAll(orderBy,                                         tx);    }
        public new CCheckList SelectWhere(string columnName,               object columnValue,                                     IDbTransaction tx)  {    return (CCheckList)base.SelectWhere(columnName,              columnValue,          tx);    }
        public new CCheckList SelectWhere(string columnName,   ESign sign, object columnValue,                                     IDbTransaction tx)  {    return (CCheckList)base.SelectWhere(columnName,    sign,     columnValue,          tx);    }
        public new CCheckList SelectWhere(                             CCriteria     criteria,                                     IDbTransaction tx)  {    return (CCheckList)base.SelectWhere(               criteria,                       tx);    }
        public new CCheckList SelectWhere(                             CCriteriaList criteria,                                     IDbTransaction tx)  {    return (CCheckList)base.SelectWhere(               criteria,                       tx);    }
        public new CCheckList SelectWhere(                             CCriteriaList criteria, string tableOrJoin,                 IDbTransaction tx)  {    return (CCheckList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public new CCheckList SelectWhere(                             CCriteriaList criteria, string tableOrJoin, string orderBy, IDbTransaction tx)  {    return (CCheckList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public     CCheckList SelectById(long checkId,                                                              IDbTransaction tx)  {    return (CCheckList)base.SelectById(checkId,                         tx);    }
        public     CCheckList SelectByIds(List<long> ids,                                                        IDbTransaction tx)  {    return (CCheckList)base.SelectByIds(ids,                                           tx);    }

        //Select Queries - Stored Procs
        public new CCheckList MakeList(string storedProcName,                             IDbTransaction txOrNull)    {   return (CCheckList)base.MakeList(storedProcName,             txOrNull);  }
        public new CCheckList MakeList(string storedProcName, object[] parameters,        IDbTransaction txOrNull)    {   return (CCheckList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CCheckList MakeList(string storedProcName, CNameValueList parameters,  IDbTransaction txOrNull)    {   return (CCheckList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CCheckList MakeList(string storedProcName, List<object> parameters,    IDbTransaction txOrNull)    {   return (CCheckList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CCheckList MakeList(string storedProcName, int param1,                 IDbTransaction txOrNull)    {   return (CCheckList)base.MakeList(storedProcName, param1,     txOrNull);  }
        public new CCheckList MakeList(string storedProcName, string param1,              IDbTransaction txOrNull)    {   return (CCheckList)base.MakeList(storedProcName, param1,     txOrNull);  }

        //Query Results
        protected new CCheckList MakeList(DataSet              ds) { return (CCheckList)base.MakeList(ds);        }
        protected new CCheckList MakeList(DataTable            dt) { return (CCheckList)base.MakeList(dt);        }
        protected new CCheckList MakeList(DataRowCollection  rows) { return (CCheckList)base.MakeList(rows);      }
        protected new CCheckList MakeList(IDataReader          dr) { return (CCheckList)base.MakeList(dr);        }
        protected new CCheckList MakeList(object           drOrDs) { return (CCheckList)base.MakeList(drOrDs);    }        
        protected new CCheckList MakeList(byte[]             gzip) { return (CCheckList)base.MakeList(gzip);      }
        #endregion

        #region Queries - SelectBy[FK] (user-nominated fk/bool columns)
        //Non-Paged
        public CCheckList SelectByUserId(long checkUserId)    {    return SelectWhere(new CCriteriaList("CheckUserId", checkUserId));    }
        public CCheckList SelectByIsIncremental(bool checkIsIncremental)    {    return SelectWhere(new CCriteriaList("CheckIsIncremental", checkIsIncremental));    }

        //Paged
        public CCheckList SelectByUserId(CPagingInfo pi, long checkUserId)    {    return SelectWhere(pi, new CCriteriaList("CheckUserId", checkUserId));    }
        public CCheckList SelectByIsIncremental(CPagingInfo pi, bool checkIsIncremental)    {    return SelectWhere(pi, new CCriteriaList("CheckIsIncremental", checkIsIncremental));    }

        //Count
        public int SelectCountByUserId(long checkUserId)   {   return SelectCount(new CCriteriaList("CheckUserId", checkUserId));     }
        public int SelectCountByIsIncremental(bool checkIsIncremental)   {   return SelectCount(new CCriteriaList("CheckIsIncremental", checkIsIncremental));     }

        //Transactional
        public CCheckList SelectByUserId(long checkUserId, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("CheckUserId", checkUserId), tx);    }
        public CCheckList SelectByIsIncremental(bool checkIsIncremental, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("CheckIsIncremental", checkIsIncremental), tx);    }
        #endregion

        #region ToXml
        protected override void ToXml_Autogenerated(System.Xml.XmlWriter w)
        {
            Store(w, "CheckId", this.CheckId);
            Store(w, "CheckUserId", this.CheckUserId);
            Store(w, "CheckStarted", this.CheckStarted);
            Store(w, "CheckFinished", this.CheckFinished);
            Store(w, "CheckInserts", this.CheckInserts);
            Store(w, "CheckUpdates", this.CheckUpdates);
            Store(w, "CheckDeletes", this.CheckDeletes);
            Store(w, "CheckApiCalls", this.CheckApiCalls);
            Store(w, "CheckIsIncremental", this.CheckIsIncremental);
            Store(w, "CheckErrors", this.CheckErrors);
        }
        #endregion

    }
}
